﻿package {
	import flash.display.*;
	import com.soulwire.media.MotionTracker;
	import Engine;
	import flash.events.*;
    import flash.net.URLRequest;
	
	public class MusicPlayerEngine extends Engine {
		
		private var bar:MusicPlayerBar;
		private var list:MusicPlayerList;
		private var displayer:MovieClip;
		private var listener:Function;
		
		private var nextBob:BubbleMovieClip;
		private var prevBob:BubbleMovieClip;
		private var backBob:BubbleMovieClip;
		private var bubbles:Array;
		
		private var playlist:Playlist;
		private var player:Player;
		
		public function MusicPlayerEngine(bobs:Array, displayer:MovieClip, 
				motion:MotionTracker, pl:Playlist) {

			this.playlist = pl;//new Playlist(new URLRequest("musikbibliotek.xml"));
			player = new Player(this.playlist);
			
			this.bubbles = bobs;
			this.displayer = displayer;
			
			prevBob = createPrev();
			nextBob = createNext();
			
			backBob = new BubbleMovieClip();
			backBob.setInitVariabels(100, 10, displayer.stage.stageHeight - 100 - 15, 
				displayer.stage.stageWidth, displayer.stage.stageHeight, "Back");
			var arr:Arrow = new Arrow();
			backBob.setMovieClip(arr, 100 / 2 - arr.width / 4 - 7, 100 / 2 - arr.height / 4);
			backBob.setDestroyedListener(
				function() {
					kill();
					displayer.gotoAndPlay(5);
				});
			backBob.setExplodeListener(
				function() {
					bar.destroy();
					list.destroy();
					for(var j:int = 0; j < bubbles.length; j++)
						bubbles[j].destroy();
				});
			
			if(bubbles == null)
				bubbles = new Array();
			
			bubbles.push(prevBob, nextBob, backBob);
			super(bubbles, displayer, motion);
			
			bar = new MusicPlayerBar(playlist.artist, playlist.album);
			bar.x = displayer.stage.stageWidth / 2 - bar.width / 2;
			bar.y = 55;
			bar.setEndListener(
				function() {
					player.nextTrack();
					list.nextItem();
					loadPlayer();
				});
			
			//displayer.addChild(bar);
			list = new MusicPlayerList(playlist.playlist());
			list.x = 20;
			list.y = 135;
			run();
		}
		
		public override function run() {
			displayer.addChild(bar);
			bar.init();
			
			displayer.addChild(list);
			list.init();
			
			displayer.addChild(prevBob);
			prevBob.init();
			
			displayer.addChild(nextBob);
			nextBob.init();
			
			displayer.addChild(backBob);
			backBob.init();
			
			listener = getListener();
			displayer.addEventListener(Event.ENTER_FRAME, listener);
			loadPlayer();
		}
		
		public override function kill() {
			cleanBox();
			displayer.removeEventListener(Event.ENTER_FRAME, listener);
			player.stopPlayer();
			bar.stopSlider();
			displayer.removeChild(bar);
			displayer.removeChild(list);
			for(var j:int = 0; j < bubbles.length; j++)
				displayer.removeChild(bubbles[j]);
		}
		
		private function loadPlayer() {
			player.setLoadCompleteListener( 
				function(){
					bar.playNew(list.getCurrent(), player.playMusic());
				});
			player.load();
		}
		
		private function removeElement(element:BubbleMovieClip) {
			for(var i:int = 0; i < bubbles.length; i++) {
				if(bubbles[i] == element) {
					bubbles.splice(i, 1);
					return;
				}
			}
		}
		
		private function createNext():BubbleMovieClip {
			var bubble:BubbleMovieClip = new BubbleMovieClip();
			bubble.setInitVariabels(100, displayer.stage.stageWidth - 10 - 100, 20, 
				displayer.stage.stageWidth, displayer.stage.stageHeight, "Next");
			
			var but1:MPButton = new MPButton();
			bubble.setMovieClip(but1, 100 / 2 - but1.width / 4, 
				100 / 2 - but1.height / 4);
			
			bubble.setDestroyedListener(
				function() {
					displayer.removeChild(nextBob);
					removeElement(nextBob);
					nextBob = createNext();
					displayer.addChild(nextBob);
					bubbles.push(nextBob);
					nextBob.init();
				});
			bubble.setExplodeListener(
				function() {
					player.nextTrack();
					list.nextItem();
					loadPlayer();
				});
			
			return bubble;
		}
		
		private function createPrev():BubbleMovieClip {
			var bubble:BubbleMovieClip = new BubbleMovieClip();
			bubble.setInitVariabels(100, 10, 20, displayer.stage.stageWidth, 
				displayer.stage.stageHeight, "Prev");
			
			var but:MPButton = new MPButton();
			but.scaleX *= -1;
			bubble.setMovieClip(but, 100 / 2 + but.width / 2, 
				100 / 2 - but.height / 4);
			
			bubble.setDestroyedListener(
				function() {
					displayer.removeChild(prevBob);
					removeElement(prevBob);
					prevBob = createPrev();
					displayer.addChild(prevBob);
					bubbles.push(prevBob);
					prevBob.init();
				});
			
			bubble.setExplodeListener(
				function() {
					player.previousTrack();
					list.prevItem();
					loadPlayer();
				});
			
			return bubble;
		}
	}
}